package com.ucar.training.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

/**
 * @author XiongMM
 * @date 2019/7/26 15:34
 * @description
 *
 * 监听器中的三个方法分别监听了Servlet上下文的参数在新增、修改、删除时的事件，如果在应用层代码中修改上下文的参数：

request.getSession().getServletContext().setAttribute("testContextAttribute", "abc");

request.getSession().getServletContext().setAttribute("testContextAttribute", "def");

request.getSession().getServletContext().removeAttribute("testContextAttribute");
监听器就可以监听到这些事件并输出：

新增上下文参数，name:testContextAttribute,value:abc

修改上下文参数，name:testContextAttribute,value:abc

删除上下文参数，name:testContextAttribute,value:def

 */
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("新增上下文参数，name:"+servletContextAttributeEvent.getName()+",value:"+servletContextAttributeEvent.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("删除上下文参数，name:"+servletContextAttributeEvent.getName()+",value:"+servletContextAttributeEvent.getValue());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        System.out.println("修改上下文参数，name:"+servletContextAttributeEvent.getName()+",value:"+servletContextAttributeEvent.getValue());
    }
}
